package com.leantech.android.browser.ui.setting

import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.leantech.android.browser.R
import com.leantech.android.browser.databinding.ActivityBrowserSettingBinding
import com.leantech.android.browser.dialog.BottomSheetDialogHelper
import com.leantech.android.browser.dialog.FontSizeBottomSheet
import com.leantech.android.browser.ui.BaseActivity
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.toast
import com.leantech.android.browser.views.SkinCheckBox
import com.qmuiteam.qmui.kotlin.skin
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

class BrowserSettingActivity : BaseActivity() {


    private val viewModel by viewModel<BrowserSettingViewModel>()

    private lateinit var binding: ActivityBrowserSettingBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBrowserSettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        collectFlow()
    }

    private fun initView() {
        binding.topBar.setTitle(R.string.text_browser_setting)
        binding.topBar.addLeftBackImageButton().onThrottleClick {
            finish()
        }

        binding.itemFontSize.onThrottleClick {
            FontSizeBottomSheet(this) { fontSize ->
                viewModel.updateFontSize(fontSize)
            }.apply {
                updateProgress(viewModel.getCurrentFontSize())
                show()
            }
        }

        binding.itemUaSetting.onThrottleClick {
            BottomSheetDialogHelper.createUASettingDialog(this,
                viewModel.userAgentList(),
                onItemClick = { ua ->
                    if (ua != null) {
                        viewModel.switchUserAgent(ua.id)
                        toast("UA已切换到【${ua.name}】")
                    }
                }).show()
        }

        binding.itemBlockIntent.onThrottleClick {
            binding.switchBlockIntent.toggle()
        }

        binding.switchBlockIntent.isChecked = viewModel.getCurrentBlockIntent()

        binding.switchBlockIntent.setOnCheckedChangeListener { _, isChecked ->
            viewModel.updateBlockIntent(isChecked)
        }

        binding.switchBlockIntent.skin {
            custom(SkinCheckBox.KeyButton, R.attr.app_skin_state_switch_checkbox)
        }

    }

    private fun collectFlow() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.CREATED) {
                launch {
                    viewModel.fontSizeFlow.collectLatest { fontSize ->
                        val displayText = if (fontSize == 100) "默认" else "$fontSize%"
                        binding.textFontSizeDesc.text = displayText
                    }
                }

                launch {
                    viewModel.currentUaFlow.collectLatest {
                        binding.textUa.text = it?.name
                    }
                }
            }
        }
    }

}